package org.example.consumer;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tpc-zpy",consumerGroup = "tpc")
public class MyConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    RedissonClient redissonClient;
    @Override
    public void onMessage(MessageExt messageExt) {
        String body = new String(messageExt.getBody());
        RLock lock = redissonClient.getLock(body);
        boolean b = lock.tryLock();
        if(b){
            try {
                System.out.println(body+"000000000000000000000");
            }finally {
                if (lock.isLocked()){
                    lock.unlock();
                }
            }
        }
    }
}
